<template>
    <el-dialog :close-on-click-modal="false" title="修改密码" :visible.sync="visible" width="32.5%" :before-close="handleClose">
        <el-form
          ref="changeForm" 
          :model="form" 
          :rules="rules" 
          status-icon
          @submit.native.prevent
        >
          <el-form-item prop="oldPassword" label="原密码">
            <el-input type="password" v-model="form.oldPassword" placeholder="原密码" @keyup.enter.native="false"/>
          </el-form-item>
          <el-form-item prop="newPassword" label="新密码">
            <el-input type="password" v-model="form.newPassword" placeholder="新密码" @keyup.enter.native="false"/>
          </el-form-item>
          <el-form-item prop="twicePasswrod" label="再次确认密码">
            <el-input type="password" v-model="form.twicePasswrod" placeholder="再次确认密码" @keyup.enter.native="false"/>
          </el-form-item>
        </el-form>
    <template #footer>
      <div class="buttons">
        <el-button type="primary" @click="handleChangePassword">确 定</el-button>
        <el-button plain @click="handleClose">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { mapActions, mapState } from 'vuex';

export default {
    name:'' ,
    components: {},
    props: {
        visible: {
            type: Boolean,
            default: false
        }
    },
    computed:{
        ...mapState({
            userInfo: state => state.user.userInfo,
        })
    },
    data() {
        let checkOld = (rule ,value, callback) => {
            if (value !== localStorage.password) {
                return callback(new Error('原密码错误，请重试。'));
            }else{
                callback();
            }
        };
        let checkTwice = (rule ,value, callback) => {
            if (value!==this.form.newPassword) {
                return callback(new Error('与新密码不一致。'));
            }else{
                callback();
            }
        };
        return {
            form:{ 
                newPassword: '',
                oldPassword: '',
                twicePasswrod: ''
            },
            rules: {
                oldPassword: [
                    { required: true, message: '原密码不能为空', trigger: 'change' },
                    { validator: checkOld, trigger: 'change' },
                ],
                newPassword: [
                    { required: true, message: '新密码不能为空', trigger: 'change' },
                ],
                twicePasswrod: [
                    { required: true, message: '再次确认密码不能为空', trigger: 'change' },
                    { validator: checkTwice, trigger: 'change' },
                ],
            },
        };
    },
    methods: {
        ...mapActions({
            fetchUpdatePassword: 'user/fetchUpdatePassword'
        }),
        handleClose(){
            Object.keys(this.form).forEach(key => {
                this.form[key] = null;
            });
            this.$emit('close');
        },
        handleChangePassword(){
            this.$refs.changeForm.validate((valid) => {
                if(valid){
                    this.fetchUpdatePassword({
                        user_id: this.userInfo.user_id,
                        oldPassWord: this.form.oldPassword,
                        user_password: this.form.newPassword,
                        cfgPwd: this.form.twicePasswrod
                    });
                    this.handleClose();
                }else{
                return false;
                }
            })
        },
    },
}
</script>
<style lang='scss' scoped src="./index.scss"/>